package com.padakeji.android.ui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnGroupClickListener;

@SuppressLint("NewApi")
public class MyTreeView extends ExpandableListView implements OnScrollListener,
		OnGroupClickListener {
	public MyTreeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		registerListener();
	}

	public MyTreeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		registerListener();
	}

	public MyTreeView(Context context) {
		super(context);
		registerListener();
	}

	/**
	 * Adapter 锟接匡拷 . 锟叫憋拷锟斤拷锟绞碉拷执私涌锟�.
	 */
	public interface MyTreeHeaderAdapter {
		public static final int PINNED_HEADER_GONE = 0;
		public static final int PINNED_HEADER_VISIBLE = 1;
		public static final int PINNED_HEADER_PUSHED_UP = 2;

		/**
		 * 锟矫碉拷group锟斤拷child 锟斤拷锟斤拷锟斤拷
		 * 
		 * @param groupPosition
		 */
		public int getChildrenCount(int groupPosition);

		/**
		 * 锟斤拷取 Header 锟斤拷状态
		 * 
		 * @param groupPosition
		 * @param childPosition
		 * @return 
		 *         PINNED_HEADER_GONE,PINNED_HEADER_VISIBLE,PINNED_HEADER_PUSHED_UP
		 *         锟斤拷锟斤拷之一
		 */
		int getTreeHeaderState(int groupPosition, int childPosition);

		/**
		 * 锟斤拷锟斤拷 Header, 锟斤拷 Header 知锟斤拷锟斤拷示锟斤拷锟斤拷锟斤拷
		 * 
		 * @param header
		 * @param groupPosition
		 * @param childPosition
		 * @param alpha
		 */
		void configureTreeHeader(View header, int groupPosition,
                                 int childPosition, int alpha);

		/**
		 * 锟斤拷锟斤拷锟介按锟铰碉拷状态
		 * 
		 * @param groupPosition
		 * @param status
		 */
		void onHeadViewClick(int groupPosition, int status);

		/**
		 * 锟斤拷取锟介按锟铰碉拷状态
		 * 
		 * @param groupPosition
		 * @return
		 */
		int getHeadViewClickStatus(int groupPosition);

		/**
		 * 为锟斤拷锟斤拷headView锟斤拷锟绞憋拷锟斤拷锟斤拷锟铰硷拷
		 * 
		 * @param groupPosition
		 */
		void headViewTimeClick(final int groupPosition);

	}

	private static final int MAX_ALPHA = 255;

	private MyTreeHeaderAdapter mAdapter;

	/**
	 * 锟斤拷锟斤拷锟斤拷锟叫憋拷头锟斤拷示锟斤拷 View,mHeaderViewVisible 为 true 锟脚可硷拷
	 */
	private View mHeaderView;

	/**
	 * 锟斤拷取mHeaderView锟叫碉拷时锟斤拷丶锟�
	 */
	private TextView mHeaderViewTime;

	/**
	 * 锟叫憋拷头锟角凤拷杉锟�
	 */
	private boolean mHeaderViewVisible;

	private int mHeaderViewWidth;

	private int mHeaderViewHeight;

	private float mHeaderViewTimeX;

	public void setHeaderView(View view) {
		mHeaderView = view;

		LayoutParams lp = new LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		view.setLayoutParams(lp);

		if (mHeaderView != null) {
			setFadingEdgeLength(0);
		}

		requestLayout();
	}

	private void registerListener() {
		setOnScrollListener(this);
		setOnGroupClickListener(this);
	}

	/**
	 * 锟斤拷锟�HeaderView 锟斤拷锟斤拷锟斤拷锟铰硷拷
	 */
	private void headerViewClick() {
		long packedPosition = getExpandableListPosition(this
				.getFirstVisiblePosition());

		int groupPosition = ExpandableListView
				.getPackedPositionGroup(packedPosition);

		if (mAdapter.getHeadViewClickStatus(groupPosition) == 1) {
			this.collapseGroup(groupPosition);
			mAdapter.onHeadViewClick(groupPosition, 0);
		} else {
			this.expandGroup(groupPosition);
			mAdapter.onHeadViewClick(groupPosition, 1);
		}

		this.setSelectedGroup(groupPosition);
	}

	private float mDownX;
	private float mDownY;

	/**
	 * 锟斤拷锟�HeaderView 锟角可硷拷锟斤拷 , 锟剿猴拷锟斤拷锟斤拷锟斤拷锟叫讹拷锟角凤拷锟斤拷锟斤拷 HeaderView, 锟斤拷锟斤拷锟斤拷锟斤拷应锟侥达拷锟斤拷 , 锟斤拷为 HeaderView
	 * 锟角伙拷锟斤拷去锟斤拷 , 锟斤拷锟斤拷锟斤拷锟斤拷锟铰硷拷锟斤拷锟斤拷锟斤拷锟斤拷效锟斤拷 , 只锟斤拷锟斤拷锟叫匡拷锟斤拷 .
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (mHeaderViewVisible) {
			switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mDownX = ev.getX();
				mDownY = ev.getY();
				if (mDownX <= mHeaderViewWidth && mDownY <= mHeaderViewHeight) {
					return true;
				}
				break;
			case MotionEvent.ACTION_UP:
				float x = ev.getX();
				float y = ev.getY();
				float offsetX = Math.abs(x - mDownX);
				float offsetY = Math.abs(y - mDownY);
				// 锟斤拷锟�HeaderView 锟角可硷拷锟斤拷 , 锟斤拷锟斤拷锟�HeaderView 锟斤拷 , 锟斤拷么锟斤拷锟斤拷 headerClick()
				if (x <= mHeaderViewWidth && y <= mHeaderViewHeight
						&& offsetX <= mHeaderViewWidth
						&& offsetY <= mHeaderViewHeight) {
					if (mHeaderView != null) {
						long packedPosition = getExpandableListPosition(this
								.getFirstVisiblePosition());
						int groupPosition = ExpandableListView
								.getPackedPositionGroup(packedPosition);

						if (x >= mHeaderViewTimeX) {
							mAdapter.headViewTimeClick(groupPosition);
						} else
							headerViewClick();
					}

					return true;
				}
				break;
			default:
				break;
			}
		}

		return super.onTouchEvent(ev);

	}

	@Override
	public void setAdapter(ExpandableListAdapter adapter) {
		super.setAdapter(adapter);
		mAdapter = (MyTreeHeaderAdapter) adapter;
	}

	/**
	 * 
	 * 锟斤拷锟斤拷锟�Group 锟斤拷锟斤拷锟斤拷锟铰硷拷 , 要锟斤拷锟捷革拷锟捷碉拷前锟斤拷锟�Group 锟斤拷状态锟斤拷
	 */
	@Override
	public boolean onGroupClick(ExpandableListView parent, View v,
			int groupPosition, long id) {
		if (mAdapter.getHeadViewClickStatus(groupPosition) == 0) {
			mAdapter.onHeadViewClick(groupPosition, 1);
			if (mAdapter.getChildrenCount(groupPosition) != 0) // 锟斤拷锟杰放碉拷if前锟芥，锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷通状态锟斤拷锟斤拷为锟斤拷始锟斤拷时锟斤拷锟斤拷锟捷的会导锟斤拷锟睫凤拷锟斤拷group
				parent.expandGroup(groupPosition);
			else
				parent.collapseGroup(groupPosition);
			parent.setSelectedGroup(groupPosition);

		} else if (mAdapter.getHeadViewClickStatus(groupPosition) == 1) {
			mAdapter.onHeadViewClick(groupPosition, 0);
			if (mAdapter.getChildrenCount(groupPosition) != 0)
				parent.collapseGroup(groupPosition);
		}

		// 锟斤拷锟斤拷 true 锟脚匡拷锟皆碉拷锟截碉拷一锟斤拷 , 锟斤拷知锟斤拷为什么
		return true;

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		if (mHeaderView != null) {
			measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec);
			mHeaderViewWidth = mHeaderView.getMeasuredWidth();
			mHeaderViewHeight = mHeaderView.getMeasuredHeight();

			mHeaderViewTimeX = mHeaderViewTime.getX();
		}
	}

	private int mOldState = -1;

	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		try {
			final long flatPostion = getExpandableListPosition(getFirstVisiblePosition());
			final int groupPos = ExpandableListView
					.getPackedPositionGroup(flatPostion);
			final int childPos = ExpandableListView
					.getPackedPositionChild(flatPostion);
			int state = mAdapter.getTreeHeaderState(groupPos, childPos);
			if (mHeaderView != null && mAdapter != null && state != mOldState) {
				mOldState = state;
				mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
			}
			configureHeaderView(groupPos, childPos);
		} catch (Exception e) {
		}
	}

	public void configureHeaderView(int groupPosition, int childPosition) {
		if (mHeaderView == null || mAdapter == null
				|| ((ExpandableListAdapter) mAdapter).getGroupCount() == 0) {
			return;
		}

		int state = mAdapter.getTreeHeaderState(groupPosition, childPosition);

		switch (state) {
		case MyTreeHeaderAdapter.PINNED_HEADER_GONE: {
			mHeaderViewVisible = false;
			break;
		}

		case MyTreeHeaderAdapter.PINNED_HEADER_VISIBLE: {
			mAdapter.configureTreeHeader(mHeaderView, groupPosition,
					childPosition, MAX_ALPHA);

			if (mHeaderView.getTop() != 0) {
				mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
			}
			mHeaderViewVisible = true;

			break;
		}

		case MyTreeHeaderAdapter.PINNED_HEADER_PUSHED_UP: {
			View firstView = getChildAt(0);
			int bottom = firstView.getBottom();

			// intitemHeight = firstView.getHeight();
			int headerHeight = mHeaderView.getHeight();

			int y;

			int alpha;

			if (bottom < headerHeight) {
				y = (bottom - headerHeight);
				alpha = MAX_ALPHA * (headerHeight + y) / headerHeight;
			} else {
				y = 0;
				alpha = MAX_ALPHA;
			}

			mAdapter.configureTreeHeader(mHeaderView, groupPosition,
					childPosition, alpha);

			if (mHeaderView.getTop() != y) {
				mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight
						+ y);
			}

			mHeaderViewVisible = true;
			break;
		}
		}
	}

	@Override
	/**
	 * 锟叫憋拷锟斤拷锟斤拷锟斤拷时锟斤拷锟矫该凤拷锟斤拷(锟斤拷锟斤拷锟绞�
	 */
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if (mHeaderViewVisible) {
			// 锟斤拷锟斤拷锟斤拷锟斤拷直锟接伙拷锟狡碉拷锟斤拷锟斤拷锟叫ｏ拷锟斤拷锟斤拷锟角硷拷锟诫到ViewGroup锟斤拷
			drawChild(canvas, mHeaderView, getDrawingTime());
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		final long flatPos = getExpandableListPosition(firstVisibleItem);
		int groupPosition = ExpandableListView.getPackedPositionGroup(flatPos);
		int childPosition = ExpandableListView.getPackedPositionChild(flatPos);

		configureHeaderView(groupPosition, childPosition);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}

}
